package com.example.fengyungo.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.fengyungo.activity.me.MyOrderDetailActivity;
import com.example.fengyungo.R;
import com.example.fengyungo.constant.OrderConstant;
import com.example.fengyungo.domain.OrderDetailsVO;

import java.util.List;

public class MyOrderAdapter  extends RecyclerView.Adapter<MyOrderAdapter.ViewHolder> {

    private static final String TAG = "MyOrderAdapter";

    protected List<OrderDetailsVO> mOrderList;

    protected Context mContext;


    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView begin;
        TextView end;
        TextView status;
        View orderView;

        public ViewHolder(View view) {
            super(view);
            orderView = view;
            begin = view.findViewById(R.id.begin);
            end = view.findViewById(R.id.end);
            status = view.findViewById(R.id.textview);
        }
    }

    public MyOrderAdapter(List<OrderDetailsVO> ordersList) {
        mOrderList = ordersList;
    }

    /**
     * 用于创建ViewHolder实例
     *
     * @param parent   ViewGroup
     * @param viewType ViewHolder
     * @return int
     */
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (mContext == null) {
            mContext = parent.getContext();
        }

        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_orders, parent, false);
        ViewHolder holder = new ViewHolder(view);

        holder.orderView.setOnClickListener(view1 -> {
            int position = holder.getLayoutPosition();
            OrderDetailsVO order = mOrderList.get(position);
            Intent intent = new Intent(mContext, MyOrderDetailActivity.class);
            intent.putExtra(OrderConstant.ORDER_DETAILS_VO, order);
            mContext.startActivity(intent);
        });

        return holder;
    }

    /**
     * 对RecyclerView子项的数据赋值
     *
     * @param holder ViewHolder
     * @param position int
     */
    @Override
    public void onBindViewHolder(MyOrderAdapter.ViewHolder holder, int position) {
        OrderDetailsVO order = mOrderList.get(position);
        holder.begin.setText(order.getBegin());
        holder.end.setText(order.getEnd());
        holder.status.setText(OrderConstant.STATUS_TEXT[order.getOrderStatus()]);
    }

    @Override
    public int getItemCount() {
        return mOrderList.size();
    }
}

